<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>{$appConfigs.title}</title>
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/{$page_theme | default: 'default'}/easyui.css">
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="{$contextPath}/pas/easyui/themes/color.css">
    <script type="text/javascript" src="{$contextPath}/pas/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="{$contextPath}/pas/easyui/jquery.easyui.min.js"></script>
</head>
<body onpageshow="resetPagination()">
    <table id="dg" title="所有栏目" class="easyui-treegrid" style="width:99%;height:95%"
            data-options="method:'post', url:'{$contextPath}/cms/category/findChildrenTree.xdo?parent_id={$Request.pid|default:'0'}',idField:'category_id',treeField:'name'"
            toolbar="#toolbar" pagination="true"
            rownumbers="true" fitColumns="true" singleSelect="true">
        <thead>
            <tr>
                <th data-options="field:'name',width:'100'" formatter="nameFormatter">名称</th>
                <th data-options="field:'code',width:'50'">代码</th>
                <th field="desc" width="250">说明</th>
                <th field="sort_id" width="30" align="right">排序</th>
                <th field="contents" width="30" align="right">内容数量</th>
            </tr>
        </thead>
    </table>
    <div id="toolbar">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="createOne()">新建</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="updateOne()">修改</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeOne()">删除</a>
    </div>
    
    <div id="dlg" class="easyui-dialog" style="width:800px; height:540px;padding:10px 20px"
            closed="true" buttons="#dlg-buttons">
        <div class="ftitle">栏目信息</div>
        <form id="fm" method="post" enctype='multipart/form-data' novalidate>
            <div class="fitem">
                <label>上级:</label>
				<select name="parent_id" id="parent_id" class="easyui-combotree" style="width:500px;"
						data-options="url:'{$contextPath}/cms/category/findChildrenTree.xdo?parent_id={$Request.pid|default:'0'}',required:false">
				</select>
            </div>
            <div class="fitem">
                <label>名称:</label>
                <input name="name" class="easyui-textbox" required="true" style="width:500px">
            </div>
            <div class="fitem">
                <label>代码:</label>
                <input name="code" class="easyui-textbox" required="true" width="500">
            </div>
            <div class="fitem" id="fileitem">
                <label>图标:</label>
                <input name="icon" type="file" style="width:500px">
            </div>
            <div class="fitem">
                <label>说明:</label>
                <input name="desc" class="easyui-textbox" multiline="true" style="width:500px;height:180px">
            </div>
            <div class="fitem">
                <label>排序:</label>
                <input name="sort_id" class="easyui-numberbox">
            </div>
            <div class="fitem">
                <label>关键字:</label>
                <input name="keywords" class="easyui-textbox" style="width:500px">
            </div>
            <div class="fitem">
                <label>扩展定义:</label>
                <input name="xfields" class="easyui-textbox" style="width:500px">
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveOne()" style="width:90px">保存</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
    </div>
    <script type="text/javascript">
    var category_id = '{$categoryObject.category_id}';
    var uploadURL = '/uploads';
    {literal}
        var url;
        function createOne(){
            $('#dlg').dialog('open').dialog('setTitle','新增');
            $('#fm').form('clear');
            url = '/cms/category/create.xdo?';
        }
        function updateOne(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $('#dlg').dialog('open').dialog('setTitle','修改');
                row['icon']=null;
                $('#fm').form('load',row);
                url = '/cms/category/update.xdo?category_id='+row.category_id;
            }
        }
        function saveOne(){
            $('#fm').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.ReturnCode == 0){
                        $.messager.show({
                            title: '提示',
                            msg: '操作成功！'
                        });
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').treegrid('reload');    	// reload the user data
						$('#parent_id').combotree('reload');
                    } else {
                        $.messager.show({
                            title: 'Error',
                            msg: result.Msg
                        });
                    }
                }
            });
        }
        function removeOne(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','删除后数据不能恢复，确认删除么?',function(r){
                    if (r){
                        $.post('/cms/category/remove.xdo',{category_id:row.category_id},function(result){
                            if (result.ReturnCode == 0){
		                        $.messager.show({
		                            title: '提示',
		                            msg: '删除成功！'
		                        });
                                $('#dg').treegrid('reload');    // reload the user data
								$('#parent_id').combotree('reload');
                            } else {
		                        $.messager.show({
		                            title: 'Error',
		                            msg: result.Msg
		                        });
                            }
                        },'json');
                    }
                });
            }
        }
		function nameFormatter(value, row, index){
			if(!value) value ="";
			if(row.icon) {
				return '<img height=\"40\" width=\"40\" src=\"' + uploadURL + "/" + row.icon+'\"/>'+value;
			} else {
				return value;
			}
		}
		function resetPagination(){
			var p = $('#dg').datagrid('getPager');
			$(p).pagination({             
				pageSize: 15,           
		 		pageList: [15,30],
				beforePageText: '第',      
				afterPageText: '页    共 {pages} 页',             
				displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'                  
			}); 
		}
    </script>
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
        .fitem input{
            width:160px;
        }
    </style>
    {/literal}
</body>
</html>